package com.gr.dmbackend.service.Impl.sysadmin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gr.dmbackend.mapper.StudentMapper;
import com.gr.dmbackend.mapper.UserMapper;
import com.gr.dmbackend.model.Student;
import com.gr.dmbackend.model.User;
import com.gr.dmbackend.service.sysadmin.AddStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AddStudentServiceImpl implements AddStudentService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;    // 加密

    @Override
    public JSONObject addStudent(Map<String, String> data) {

        JSONObject resp = new JSONObject();

        String studentId = data.get("studentId");
        String password = data.get("password");
        String name = data.get("name");
        String gender = data.get("gender");
        String grade = data.get("grade");
        String building = data.get("building");
        String floor = data.get("floor");
        String dormId = data.get("dormId");
        String encodePassword = passwordEncoder.encode(password);

        if (studentId.isEmpty()) {
            resp.put("error_message", "学号不能为空");
            return resp;
        }
        if (password.isEmpty()) {
            resp.put("error_message", "密码不能为空");
            return resp;
        }
        if (name.isEmpty()) {
            resp.put("error_message", "姓名不能为空");
            return resp;
        }
        if (gender.isEmpty()) {
            resp.put("error_message", "性别不能为空");
            return resp;
        }
        if (grade.isEmpty()) {
            resp.put("error_message", "班级不能为空");
            return resp;
        }
        if ("请选择楼栋".equals(building)) {
            resp.put("error_message", "请选择楼栋");
            return resp;
        }
        if ("请选择楼层".equals(floor)) {
            resp.put("error_message", "请选择楼层");
            return resp;
        }
        if ("请选择寝室号".equals(dormId)) {
            resp.put("error_message", "请选择寝室号");
            return resp;
        }
        if (studentId.length() > 20) {
            resp.put("error_message", "学号长度超出限制");
            return resp;
        }
        if (encodePassword.length() > 255) {
            resp.put("error_message", "密码长度超出限制");
            return resp;
        }
        if (name.length() > 10) {
            resp.put("error_message", "姓名长度超出限制");
            return resp;
        }
        if (gender.length() > 10) {
            resp.put("error_message", "性别长度超出限制");
            return resp;
        }
        if (grade.length() > 20) {
            resp.put("error_message", "班级长度超出限制");
            return resp;
        }
        if (floor.length() > 5) {
            resp.put("error_message", "楼层长度超出限制");
            return resp;
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", studentId);
        List<User> users = userMapper.selectList(queryWrapper);
        if (!users.isEmpty()) {
            resp.put("error_message", "用户已存在");
            return resp;
        }

        String photo = "https://cdn.acwing.com/media/user/profile/photo/354935_lg_fb27421878.jpg";
        User user = new User();
        user.setUsername(studentId);
        user.setPassword(encodePassword);
        user.setPhoto(photo);
        user.setIdentity("student");
        userMapper.insert(user);
        int userId = user.getId();
        Student student = new Student();
        student.setUserId(userId);
        student.setStudentId(studentId);
        student.setName(name);
        student.setGender(gender);
        student.setGrade(grade);
        student.setBuilding(building);
        student.setFloor(floor);
        student.setDormId(dormId);
        studentMapper.insert(student);
        resp.put("error_message", "success");

        return resp;
    }
}
